package com.jame.concurrency.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author : Jame
 * @date : 2021-07-13 16:24
 **/
public class Lock {

    static volatile Object o = new Object();


    //创建一个公平锁
    static ReentrantLock reentrantLock = new ReentrantLock(true);

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            service();
        });
        thread.setName("A");

        Thread thread1 = new Thread(() -> {
            service();
        });
        thread1.setName("B");

        thread.start();
        Thread.sleep(1000);
        thread1.start();

        Thread.sleep(7000);
        thread1.interrupt();

    }

    public static void service() {

        try {
            reentrantLock.lockInterruptibly();
            reentrantLock.lock();
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"--==='");
        try {
            Thread.sleep(2000000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("=="+Thread.currentThread().getName());
        reentrantLock.unlock();
    }
}
